package com.climber.android.commonres.ui.preview

import android.Manifest
import android.content.Intent
import android.text.TextUtils
import android.view.View
import cn.jzvd.Jzvd
import com.bumptech.glide.Glide
import com.climber.android.commonres.R
import com.climber.android.commonres.helper.BottomSheetDialogHelper
import com.climber.android.commonres.helper.onClick
import com.climber.android.commonres.ui.BaseMVPFragment
import com.climber.android.commonres.ui.DownloadFileDialogActivity
import io.ganguo.library.mvp.ui.mvp.IPresenter
import io.ganguo.library.mvp.ui.mvp.di.component.AppComponent
import io.ganguo.library.mvp.util.FileUtils
import kotlinx.android.synthetic.main.com_fragment_media_preview_video.*

/**
 * Created by Mikes at 2020-04-17 15:28
 */
class MediaVideoPreviewFragment : BaseMVPFragment<IPresenter>() {
    private val mMediaPreview by lazy {
        arguments?.getParcelable<MediaPreview>("mediaPreview")
    }

    override fun setupFragmentComponent(appComponent: AppComponent) {
    }

    override fun getLayoutResourceId(): Int = R.layout.com_fragment_media_preview_video

    override fun initView(view: View) {
        Jzvd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_ADAPTER)
    }

    override fun initListener() {
        fl_dismiss.onClick {
            activity?.onBackPressed()
        }

        moreMenuBtn.onClick {
            downloadVideo()
        }
    }

    override fun initData() {
        val mediaPreview = mMediaPreview ?: return

        if (!TextUtils.isEmpty(mediaPreview.previewPath) && FileUtils.checkFilePathExists(mediaPreview.previewPath)) {
            jcVideoPlayer.setUp(mediaPreview.previewPath, "", Jzvd.SCREEN_FULLSCREEN)
        } else if (!TextUtils.isEmpty(mediaPreview.previewUrl)) {
            jcVideoPlayer.setUp(mediaPreview.previewUrl, "", Jzvd.SCREEN_FULLSCREEN)
        }

        if (!TextUtils.isEmpty(mediaPreview.videoThumbUrl)) {
            Glide.with(this)
                    .load(mediaPreview.videoThumbUrl)
                    .into(jcVideoPlayer.posterImageView)
        }
    }

    override fun onFragmentVisibleChange(isVisible: Boolean) {
        super.onFragmentVisibleChange(isVisible)

        if (!isVisible) {
            Jzvd.releaseAllVideos()
        }
    }

    override fun onPause() {
        super.onPause()
        Jzvd.releaseAllVideos()
    }

    private fun downloadVideo() {
        val mediaPreview = mMediaPreview ?: return

        var videoUrl = ""
        if (!TextUtils.isEmpty(mediaPreview.previewPath)) {
            videoUrl = mediaPreview.previewPath
        } else if (!TextUtils.isEmpty(mediaPreview.previewUrl)) {
            videoUrl = mediaPreview.previewUrl
        }
        if (TextUtils.isEmpty(videoUrl)) {
            return
        }

        requestSinglePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) {grant ->
            if (grant) {
                BottomSheetDialogHelper.showOnlyTitleDialog(mHostContext, "保存视频？") { confirm ->
                    if (confirm) {
                        doSaveVideo(videoUrl)
                    }
                }
            }
        }
    }

    private fun doSaveVideo(videoUrl: String) {
        startActivity(Intent(mHostContext, DownloadFileDialogActivity::class.java)
                .putExtra("downloadUrl", videoUrl))
        activity?.overridePendingTransition(R.anim.cbase__slide_in_from_bottom, 0)
    }
}